﻿using System.Windows;
using System.Windows.Input;
using Actions = AEM.Data.BLL.Actions;
using AEM.Utils;

namespace AEM.Basic
{
    /// <summary>
    /// Interaction logic for PassWord.xaml
    /// </summary>
    public partial class PassWord : Window
    {
        public PassWord()
        {
            InitializeComponent();
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(PassWord_MouseLeftButtonDown);
            this.password_btn_save.Click += new RoutedEventHandler(password_btn_save_Click);
            this.password_btn_reset.Click += new RoutedEventHandler(password_btn_reset_Click);
        }

        void PassWord_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void password_btn_save_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            if (string.IsNullOrEmpty(passWordOld.Password.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("PasswordOld") as string); return; }
            if (string.IsNullOrEmpty(passWordNew.Password.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("PasswordNew") as string); return; }
            if (string.IsNullOrEmpty(passWordReply.Password.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("PasswordReply") as string); return; }
            if (passWordNew.Password.Trim() != passWordReply.Password.Trim()) { errmsg.Text = this.TryFindResource("ComparePassword") as string; return; }

            if (!this.passWordNew.Password.checkPwd()) { errmsg.Text = this.TryFindResource("FormatPassword") as string; return; }


            Actions.User.ModifiyUserResult ret = Actions.User.ModifiyPassword(passWordOld.Password.Trim(), passWordNew.Password.Trim());
            switch (ret)
            {
                case Actions.User.ModifiyUserResult.WrongPassword:
                    errmsg.Text = this.TryFindResource("CompareOldPassword") as string;
                    break;
                case Actions.User.ModifiyUserResult.OK:
                    errmsg.Text = this.TryFindResource("successfulupdate") as string;
                    Actions.Log.AddOperation.Invoke("=InfoChangePassword");
                    passWordOld.Password = "";
                    passWordNew.Password = "";
                    passWordReply.Password = "";
                    break;
            }
        }

        void password_btn_reset_Click(object sender, RoutedEventArgs e)
        {
            if (!Common.PermissionManager.hasPower((int)Data.Enum.ControlPoints.ResetPassword))
            {
                MessageBox.Show((string)TryFindResource("PermissionDenied"));
                return;
            }
            resetmsg.Text = "";
            if (string.IsNullOrEmpty(passwordReset.Text.Trim())) { resetmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("PasswordReset") as string); return; }

            Actions.User.ModifiyUserResult ret = Actions.User.ResetPassword(passwordReset.Text.Trim());
            switch (ret)
            {
                case Actions.User.ModifiyUserResult.UserIsAdmin:
                    resetmsg.Text = "超级管理员不可重置";
                    break;
                case Actions.User.ModifiyUserResult.UserIsNotExist:
                    resetmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("PasswordReset") as string);
                    break;
                case Actions.User.ModifiyUserResult.OK:
                    errmsg.Text = this.TryFindResource("successfulupdate") as string;
                    string msg = "=InfoResetPassword," + passwordReset.Text.Trim();
                    Actions.Log.AddOperation.Invoke(msg);//string.Format((string)this.TryFindResource("InfoResetPassword"), passwordReset.Text.Trim()));
                    break;
            }
        }
    }
}
